//
//  AddViewController.m
//  Account
//
//  Created by wanmeizty on 2016/11/29.
//  Copyright © 2016年 wanmeizty. All rights reserved.
//

#import "AddViewController.h"

#import "XMCommonPicker.h"

#import "XMTitleAndValueView.h"

#import "MyButton.h"

#import "UIColor+RGB.h"

@interface AddViewController ()<UITextFieldDelegate,XMCommonPickerDelegate>{
    
    NSInteger _selectedNameIndex;
}

@property (nonatomic, strong) XMTitleAndValueView * nameText;

@property (nonatomic ,strong) XMTitleAndValueView * moneyText;

@property (nonatomic ,strong) XMTitleAndValueView * descText;

//@property (nonatomic ,strong) UITextView * descText;

@property (nonatomic, strong) XMCommonPicker * namePicker;

@property (strong, nonatomic) NSMutableArray * nameArray;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"增加记账";
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat top = 10 * [XMScreenAdaptation adapterMultipleByWidth];
    
    self.nameText = [[XMTitleAndValueView alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth], top, self.view.frame.size.width - 20 * [XMScreenAdaptation adapterMultipleByWidth], 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.nameText setupData:@"姓名" value:nil placeHolder:@"选择记账人"];
    self.nameText.getValueTextField.delegate = self;
    [self.rootScrollView addSubview:self.nameText];
    
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth];
    
    self.moneyText = [[XMTitleAndValueView alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth], top, self.view.frame.size.width - 20 * [XMScreenAdaptation adapterMultipleByWidth], 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.moneyText setupData:@"金额" value:nil placeHolder:@"请输入金额(元)"];
    [self.rootScrollView addSubview:self.moneyText];
    
    
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth] + 10 * [XMScreenAdaptation adapterMultipleByWidth];
    
    
    self.descText = [[XMTitleAndValueView alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth], top, self.view.frame.size.width - 20 * [XMScreenAdaptation adapterMultipleByWidth], 44 * [XMScreenAdaptation adapterMultipleByWidth])];
    [self.descText setupData:@"购买详细" value:nil placeHolder:@"请输入此次购买详细"];
    self.descText.getValueTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.rootScrollView addSubview:self.descText];
//    UILabel * descLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth], top, self.view.frame.size.width - 20 * [XMScreenAdaptation adapterMultipleByWidth], 30 * [XMScreenAdaptation adapterMultipleByWidth])];
//    descLabel.text = @"购买详细";
//    descLabel.textAlignment = NSTextAlignmentCenter;
//    [self.rootScrollView addSubview:descLabel];
//    
//    top += 30 * [XMScreenAdaptation adapterMultipleByWidth] + 10  * [XMScreenAdaptation adapterMultipleByWidth];
//    
//    self.descText = [[UITextView alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth], top, self.view.frame.size.width - 20 * [XMScreenAdaptation adapterMultipleByWidth], 120 * [XMScreenAdaptation adapterMultipleByWidth])];
//    self.descText.backgroundColor = [UIColor colorWithRGB:0xe0e0e0];
//    [self.rootScrollView addSubview:self.descText];
    
    top += 44 * [XMScreenAdaptation adapterMultipleByWidth] + 10  * [XMScreenAdaptation adapterMultipleByWidth];
    
    UILabel * joinLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth], top, self.view.frame.size.width - 20 * [XMScreenAdaptation adapterMultipleByWidth], 30 * [XMScreenAdaptation adapterMultipleByWidth])];
    joinLabel.text = @"选择参与人";
    joinLabel.textAlignment = NSTextAlignmentCenter;
    [self.rootScrollView addSubview:joinLabel];
    
    top += 30 * [XMScreenAdaptation adapterMultipleByWidth] ;

    NSArray *nameArr = @[@"张庭勇", @"张帆", @"陈伟豪", @"王勇", @"周建"];
    
    self.nameArray = [NSMutableArray arrayWithArray:nameArr];
    
    int i = 0;
    
    for (NSString * name in nameArr) {
        
        MyButton * joinBtn = [[MyButton alloc] initWithFrame:CGRectMake(10 * [XMScreenAdaptation adapterMultipleByWidth] + i * 60 * [XMScreenAdaptation adapterMultipleByWidth], top, 55 * [XMScreenAdaptation adapterMultipleByWidth], 30 * [XMScreenAdaptation adapterMultipleByWidth])];
        joinBtn.selected = YES;
        joinBtn.titleLabel.font = [UIFont systemFontOfSize:14 * [XMScreenAdaptation adapterMultipleByWidth]];
        [joinBtn setTitle:name forState:UIControlStateNormal];
        [joinBtn addTarget:self action:@selector(selectName:) forControlEvents:UIControlEventTouchUpInside];
        [self.rootScrollView addSubview:joinBtn];
        joinBtn.tag = i;
        i ++;
    }
    
    
    top += 30 * [XMScreenAdaptation adapterMultipleByWidth] + 30  * [XMScreenAdaptation adapterMultipleByWidth];
    
    
    MyButton * sureBtn = [[MyButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.5 - 40 * [XMScreenAdaptation adapterMultipleByWidth], top, 80 * [XMScreenAdaptation adapterMultipleByWidth], 30 * [XMScreenAdaptation adapterMultipleByWidth])];
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.rootScrollView addSubview:sureBtn];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    
    [self.rootScrollView addGestureRecognizer:tap];
    
    // Do any additional setup after loading the view.
}

- (void)tap{
    
    [self bankCardTap];
    
}

- (void)selectName:(MyButton *)btn{
    
    NSArray * originArray = @[@"张庭勇", @"张帆", @"陈伟豪", @"王勇", @"周建"];
    NSLog(@"%d",btn.selected);
    
    if (btn.selected) {
        
        btn.backgroundColor = [UIColor lightGrayColor];
        btn.selected = NO;
        [self.nameArray removeObject:originArray[btn.tag]];
        
    }else{
        [self.nameArray addObject:originArray[btn.tag]];
        btn.selected = YES;
        btn.backgroundColor = [UIColor colorWithRGB:0xF36F54];
        
    }
}

- (void)sureClick{
    [self bankCardTap];
    
    NSLog(@"%@",self.nameArray);
    
    if (self.nameText.getValueTextField.text.length <= 0) {
        
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"姓名未填写" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
        
        [alert show];
        
        return;
    }
    
    if (self.moneyText.getValueTextField.text.length <= 0) {
        
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"金额未填写" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
        
        [alert show];
        
        return;
    }
    
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:nowDate];

    NSLog(@"dateString==>%@",dateString);
    
    AccountModel * model = [[AccountModel alloc] init];
    
    model.name = self.nameText.getValueTextField.text;
    model.fee = self.moneyText.getValueTextField.text;
    model.date = dateString;
    model.desc = self.descText.getValueTextField.text;
    model.joinPerson = [self.nameArray componentsJoinedByString:@","];
    
    NSLog(@"%@--%@--%@--%@",model.name,model.fee,model.date,model.desc);
    
    [[FMDBManager shareManager] createTableWithTableName:@"accountTable"];
    
    BOOL insert = [[FMDBManager shareManager] insertWithAccountModel:model];
    
    if (insert) {
        
        [self clearData];
        UIAlertController* alertController = [UIAlertController alertControllerWithTitle:nil message:@"插入数据成功,是否退出当前页面"  preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:sureAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else{
        
        
    }
    
}

- (void)clearData{
    
    [self.nameText setupData:@"姓名" value:nil placeHolder:@"选择记账人"];
    [self.moneyText setupData:@"金额" value:nil placeHolder:@"请输入金额(元)"];
    [self.descText setupData:@"购买详细" value:nil placeHolder:@"请输入此次购买详细"];
}

/**
 <#Description#>
 
 @param textField <#textField description#>
 
 @return <#return value description#>
 */
-  (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (textField == self.nameText.getValueTextField) {
        
        [self onExpriseImageViewClicked];
        
        return NO;
    }
    
    return YES;
    
}

- (void)onExpriseImageViewClicked{
    
    [self bankCardTap];
    
    //    if (!self.ValidPicker) {
    
    NSArray *nameArr = @[@"张庭勇", @"张帆", @"陈伟豪", @"王勇", @"周建"];
    
    self.namePicker = [[XMCommonPicker alloc] initWithFrame:[UIScreen mainScreen].bounds component:1];
    self.namePicker.commonPickerDelegate = self;
    self.namePicker.dataArr = nameArr;

    _selectedNameIndex = 0;
    [self.namePicker setCurrentIndex:_selectedNameIndex subIndex:0];
//    [self.namePicker setCurrentIndex:_selectedMonthIndex subIndex:_selectedYearIndex];
    [self.namePicker show:self.rootScrollView];
    
    
}

#pragma mark - XMCommonPickerDelegate

- (void)picker:(XMCommonPicker *)picker selectedRow:(NSInteger)selectedRow selectedSubRow:(NSInteger)selectedSubRow {
    
    
    NSArray *nameArr = @[@"张庭勇", @"张帆", @"陈伟豪", @"王勇", @"周建"];
    [self.nameText setupValue:nameArr[selectedRow]];
    _selectedNameIndex = selectedRow;

    
    
    
}

- (void)bankCardTap{
    
    [[self.nameText getValueTextField] resignFirstResponder];
    [[self.moneyText getValueTextField] resignFirstResponder];
    [[self.descText getValueTextField] resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
